var tpl2  =  $("#myTemplate").html();
$.ajax({
    url:"/assets/script/calendar-list.hbs",
    type:"get",
    async:false,
    success:function(data){
        tpl2=data;
    }
});
var template2 = Handlebars.compile(tpl2);
$.ajax({
    url:'calendar-list.json',
    type: 'get',
    dataType:"json",
    success: function (data) {
        var html = template2(data);
        $('.project-content').empty();
        $('.project-content').append(html);
    },
    error:function (msg) {
        console.log(msg)
    }
});




var tpl1 = $("#myTemplate").html();
$.ajax({
    url:"/assets/script/pagination.hbs",
    type:"get",
    async:false,
    success:function(data){
        tpl1 = data;
    },
    error:function(err){

    }
});
var template1 = Handlebars.compile(tpl1);
$.ajax({
    url:'calendar-list.json',
    type:'get',
    dataType:'json',
    success:function(data){
        var array=[];
        for(var i=1;i<=data.data.totalPages;i++){
            array.push(i);
        }
        data.array=array;
        var html = template1(data);
        $('.pagina').append(html);
        $('.real a').click(function(){
            $('.pages').css('display','block');
        });
        var total = data.data.totalPages;
        $('.real a').data('total',total);
        $(document).click(function(){
            $('.pages').css('display','none');
        });
        $(".pages").click(function(event){
            event.stopPropagation();
        });
        $('.real a').click(function(event){
            event.stopPropagation();
        });
        //中间页数
        $('.page a').click(function(){
            $('.real a').html($(this).html());
            $('.pages').css('display','none');
            var current = $(this).data('p');
            $('.real a').data('p',current);
            $.ajax({
                url:'calendar-list.json',
                type:'get',
                data:{
                    currentPage:current
                },
                success:function(data){
                    var html = template2(data);
                    $('.project-content').empty();
                    $('.project-content').append(html);
                },
                error:function(msg){
                }
            })
        });
        //上一页
        $('.prev').click(function(){
            var current = Number($('.real a').data('p'))-1;
            if(current<1){
                current = 1;
                $('.real a').data('p',current);
                $('.real a').html('第'+current+'页');
            }else{
                $('.real a').data('p',current);
                $('.real a').html('第'+current+'页');
                $.ajax({
                    url:'calendar-list.json',
                    type:'get',
                    data:{
                        currentPage:current
                    },
                    success:function(data){
                        var html = template2(data);
                        $('.project-content').empty();
                        $('.project-content').append(html);
                    },
                    error:function(msg){
                    }
                })
            }

        });
        //下一页
        $('.next').click(function(){
            var current = Number($('.real a').data('p'))+1;
            var total = $('.real a').data('total');
            if(current>total){
                current = total;
                $('.real a').data('p',current);
                $('.real a').html('第'+current+'页');
            }else{
                $('.real a').data('p',current);
                $('.real a').html('第'+current+'页');
                $.ajax({
                    url:'calendar-list.json',
                    type:'get',
                    data:{
                        currentPage:current
                    },
                    success:function(data){
                        var html = template2(data);
                        $('.project-content').empty();
                        $('.project-content').append(html);
                    },
                    error:function(msg){
                    }
                })
            }

        });
    },
    error:function(){

    }
});